import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:date_format/date_format.dart';

const double kPickerHeight = 216.0;
const double kItemHeight = 40.0;
const Color kBtnColor = Color(0xFF323232); //50
const Color kTitleColor = Color(0xFF787878); //120
const double kTextFontSize = 17.0;

typedef StringClickCallback = void Function(int selectIndex, Object selectStr);
typedef ArrayClickCallback = void Function(
    List<int> selecteds, List<dynamic> strData);
typedef DateClickCallback = void Function(
    dynamic selectDateStr, dynamic selectDate);

enum DateType {
  // ignore: constant_identifier_names
  YMD, // y, m, d
  // ignore: constant_identifier_names
  YM, // y ,m
  // ignore: constant_identifier_names
  YMD_HM, // y, m, d, hh, mm
  // ignore: constant_identifier_names
  YMD_AP_HM, // y, m, d, ap, hh, mm
}

class PickerTools {
  //** 单列*/
  static void showStringPicker<T>(BuildContext context, {
    required List<T> data,
    String? leftText,
    String? title,
    String? rightText,
    int? normalIndex,
    PickerDataAdapter? adapter,
    required StringClickCallback clickCallBack,
    bool? isCenter = false,// 是否居中展示
  }) {
    openModalPicker(context,
        adapter: adapter ?? PickerDataAdapter(pickerData: data, isArray: false),
        clickCallBack: (Picker picker, List<int> selecteds) {
      //          print(picker.adapter.text);
      clickCallBack(selecteds[0], data[selecteds[0]]!);
    }, selecteds: [normalIndex ?? 0],
      leftText: leftText,
      title: title,
      rightText: rightText,
      isCenter: isCenter,
    );
  }

  //** 多列 */
  static void showArrayPicker<T>(
    BuildContext context, {
    required List<T> data,
    required String title,
    required List<int> normalIndex,
    required PickerDataAdapter adapter,
    required ArrayClickCallback clickCallBack,
  }) {
    // openModalPicker(context,
    //     adapter: adapter ?? PickerDataAdapter(pickerData: data, isArray: true),
    //     clickCallBack: (Picker picker, List<int> selecteds) {
    //   clickCallBack(selecteds, picker.getSelectedValues());
    // }, selecteds: normalIndex, title: title);
  }

  static void openModalPicker(BuildContext context, {
    required PickerAdapter adapter,
    required PickerConfirmCallback clickCallBack,
    String? leftText,
    String? title,
    String? rightText,
    List<int>? selecteds,
    bool? isCenter = false,// 是否居中展示
  }) {

    if (isCenter == true) {
      showDialog(context: context,
        builder: (BuildContext context) {
          return Center(
            child: PickerWidget(
              leftText: leftText,
              title: title,
              rightText: rightText,
              selecteds: selecteds,
              adapter: adapter,
              clickCallBack: clickCallBack,
            ),
          );
        },
      );
    } else {
      showModalBottomSheet(context: context,
        // 设置圆角
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20),
            topRight: Radius.circular(20),
          ),
        ),
        // 抗锯齿
        clipBehavior: Clip.antiAlias,
        builder: (BuildContext context) {
          return PickerWidget(
            leftText: leftText,
            title: title,
            rightText: rightText,
            selecteds: selecteds,
            adapter: adapter,
            clickCallBack: clickCallBack,
          );
        },
      );

    }
  }

  //** 日期选择器*/
  static void showDatePicker(BuildContext context, {
    DateType? dateType,
    String? title,
    DateTime? maxValue,
    DateTime? minValue,
    DateTime? value,
    DateTimePickerAdapter? adapter,
    required DateClickCallback clickCallback,
    bool? isCenter = false,// 是否居中展示
  }) {
    int timeType;
    if (dateType == DateType.YM) {
      timeType = PickerDateTimeType.kYM;
    } else if (dateType == DateType.YMD_HM) {
      timeType = PickerDateTimeType.kYMDHM;
    } else if (dateType == DateType.YMD_AP_HM) {
      timeType = PickerDateTimeType.kYMD_AP_HM;
    } else {
      timeType = PickerDateTimeType.kYMD;
    }

    openModalPicker(context,
        adapter: adapter ??
            DateTimePickerAdapter(
              type: timeType,
              isNumberMonth: true,
              yearSuffix: "年",
              monthSuffix: "月",
              daySuffix: "日",
              strAMPM: const ["上午", "下午"],
              maxValue: maxValue,
              minValue: minValue,
              value: value ?? DateTime.now(),
            ),
        title: title, isCenter: isCenter, clickCallBack: (Picker picker, List<int> selecteds) {
      var time = (picker.adapter as DateTimePickerAdapter).value;
      String timeStr;
      if (dateType == DateType.YM) {
        // ignore: prefer_interpolation_to_compose_strings
        timeStr = time!.year.toString() + "年" + time.month.toString() + "月";
      } else if (dateType == DateType.YMD_HM) {
        // ignore: prefer_interpolation_to_compose_strings
        timeStr = time!.year.toString() +
            "年" +
            time.month.toString() +
            "月" +
            time.day.toString() +
            "日" +
            time.hour.toString() +
            "时" +
            time.minute.toString() +
            "分";
      } else if (dateType == DateType.YMD_AP_HM) {
        var str = formatDate(time!, [am]) == "AM" ? "上午" : "下午";
        // ignore: prefer_interpolation_to_compose_strings
        timeStr = time.year.toString() +
            "年" +
            time.month.toString() +
            "月" +
            time.day.toString() +
            "日" +
            str +
            time.hour.toString() +
            "时" +
            time.minute.toString() +
            "分";
      } else {
        // ignore: prefer_interpolation_to_compose_strings
        timeStr = time!.year.toString() +
            "年" +
            time.month.toString() +
            "月" +
            time.day.toString() +
            "日";
      }
      clickCallback(timeStr, picker.adapter.text);
    });
  }
}

/// 自定义PickerWidget
class PickerWidget extends StatefulWidget {
  const PickerWidget({
    Key? key,
    this.data,
    this.leftText,
    this.title,
    this.rightText,
    this.selecteds,
    required this.adapter,
    this.clickCallBack,
  }) : super(key: key);

  final List? data;
  final String? leftText;
  final String? title;
  final String? rightText;
  final List<int>? selecteds;
  final PickerAdapter adapter;
  final PickerConfirmCallback? clickCallBack;

  @override
  State<PickerWidget> createState() => _PickerWidgetState();
}

class _PickerWidgetState extends State<PickerWidget> {

  @override
  Widget build(BuildContext context) {
    return _buildWidget();
  }

  _buildWidget() {
    var picker = Picker(
        adapter: widget.adapter,
        title: Text(widget.title ?? "请选择",
            style: const TextStyle(
                color: kTitleColor, fontSize: kTextFontSize)),
        selecteds: widget.selecteds,
        cancelText: widget.leftText??'取消',
        confirmText: widget.rightText??'确定',
        cancelTextStyle:
        const TextStyle(color: kBtnColor, fontSize: kTextFontSize),
        confirmTextStyle:
        const TextStyle(color: kBtnColor, fontSize: kTextFontSize),
        textAlign: TextAlign.right,
        itemExtent: kItemHeight,
        height: kPickerHeight,
        selectedTextStyle: const TextStyle(color: Colors.black),
        onConfirm: widget.clickCallBack,
    );
    return picker.makePicker();
  }
  
}